# coding: utf-8
from django.shortcuts import render_to_response, get_object_or_404
from themes.forms import *
from themes.models import Theme
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.contrib.auth.models import User
from util import build_form, render_to
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse

# --
# add_theme
# > request             instance            HttpRequest instance
# < render_to_response  instance            HttpResponse instance    
@login_required
@render_to( 'themes/add_theme.html' )
def add_theme( request ):
    if 'POST' == request.method:
        form = build_form ( AddThemeForm, request, user = request.user )
        if form.is_valid():
            theme = form.save()
            return HttpResponseRedirect( theme.get_absolute_url() )
    else:
        form = AddThemeForm()
    return { 'form': form, }

@login_required
@render_to( 'themes/edit_theme.html' )
def edit_theme( request, theme_id ):
    theme = get_object_or_404( Theme, pk = theme_id )
    if theme.user == request.user:
         form = build_form( EditThemeForm, request, user = request.user, instance = theme )
         if form.is_valid():
            theme = form.save( commit = False )
            theme.save()
            return HttpResponseRedirect( theme.get_absolute_url() )
    else:
            return HttpResponseRedirect( theme.get_absolute_url() )
    return { 'form': form,
             'theme': theme,
           }

@render_to( 'themes/show_theme.html' )
def show_theme( request, theme_id ):
    theme = get_object_or_404( Theme.objects.select_related(), pk = theme_id )
    return { 'theme': theme, }

@render_to( 'themes/show_topic.html' )
def show_topic( request, topic_id ):
    topic = get_object_or_404( Topic.objects.select_related(), pk = topic_id )
    #if not topic.forum.category.has_access(request.user):
    #    return HttpResponseForbidden()
    posts = topic.posts.all().select_related()
    users = set( post.user.id for post in posts )
    form = AddPostForm( topic = topic )
    return {
             'topic': topic,
             'form': form,
             'posts': posts,
           }


@login_required
@render_to('themes/add_post.html')
def add_post( request, theme_id, topic_id ):
    theme = None
    topic = None
    posts = None
    if theme_id:
        theme = get_object_or_404(Theme, pk=theme_id)
    elif topic_id:
        topic = get_object_or_404(Topic, pk=topic_id)
        posts = topic.posts.all().select_related()
    if topic:
        return HttpResponseRedirect( topic.get_absolute_url() )
    form = build_form( AddPostForm, request, topic = topic, theme = theme,
                       user = request.user )
    if 'post_id' in request.GET:
        post_id = request.GET['post_id']
        post = get_object_or_404( Post, pk = post_id)
    if form.is_valid():
        post = form.save();
        return HttpResponseRedirect( post.get_absolute_url() )
    return { 'form': form,
             'posts': posts,
             'topic': topic,
             'theme': theme,
           }

def show_post(request, post_id):
    post = get_object_or_404(Post, pk=post_id)
    count = post.topic.posts.filter(created__lt=post.created).count() + 1
    url = '%s#post-%d' % (reverse('topic', args=[post.topic.id]), post.id)
    return HttpResponseRedirect(url)

